package com.love.marriage.userInfo.controller;

import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.github.xiaoymin.knife4j.annotations.ApiSort;
import com.love.marriage.common.interfaces.avoid.LogRestRequest;
import com.love.marriage.common.vo.Result;
import com.love.marriage.common.vo.ResultBoolean;
import com.love.marriage.userInfo.service.LoginService;
import com.love.marriage.userInfo.vo.ResultUserVo;
import com.love.marriage.userInfo.vo.UserVo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

/**
 * description:  用户表restApi
 * author:       mybatisPlus
 * createDate:   2024-10-11 22:04:00
 * version:      1.0.0
 */
@ApiSort(10)
@Tag(name = "用户表相关接口", description = "用户表相关接口")
@RestController
@RequiredArgsConstructor
@RequestMapping("/")
public class LoginController {

    private final LoginService loginService;

    @ApiOperationSupport(order = 10, author = "alex")
    @Operation(summary = "用户密码登录", description = "用户密码登录")
    @LogRestRequest(apiName = "用户密码登录")
    @Parameters({
            @Parameter(name = "username", description = "用户名称", required = true,
                    schema = @Schema(implementation = String.class, type = "String", example = "admin")),
            @Parameter(name = "password", description = "密码", required = true,
                    schema = @Schema(implementation = String.class, type = "String", example = "admin")),
            @Parameter(name = "instrId", description = "介绍人id", required = false,
                    schema = @Schema(implementation = String.class, type = "Long", example = "admin")),
            @Parameter(name = "source", description = "来源（gzh:表示公众号来源）", required = false,
                    schema = @Schema(implementation = String.class, type = "String", example = "web"))
    })
    @ApiResponse(responseCode = "200", description = "Success",
            content = @Content(schema = @Schema(implementation = ResultUserVo.class)))
    @GetMapping(value = "/loginCode")
    @Tag(name = "登录", description = "登录相关接口")
    public Result<UserVo> login(HttpServletRequest request,
                                @RequestParam("username") String username,
                                @RequestParam("password") String password,
                                @RequestParam(value = "instrId", required = false) Long instrId,
                                @RequestParam(value = "source", required = false) String source) throws Exception {
        return Result.success(loginService.login(request, username, password, false, instrId, source));
    }

    @ApiOperationSupport(order = 12, author = "alex")
    @Operation(summary = "手机号验证码登录", description = "手机号验证码登录")
    @LogRestRequest(apiName = "手机号验证码登录")
    @Parameters({
            @Parameter(name = "phone", description = "手机号", required = true,
                    schema = @Schema(implementation = String.class, type = "String", example = "18288888888")),
            @Parameter(name = "code", description = "验证码", required = true,
                    schema = @Schema(implementation = String.class, type = "String", example = "123456")),
            @Parameter(name = "wechatCode", description = "微信编码", required = false,
                    schema = @Schema(implementation = String.class, type = "String", example = "123456")),
            @Parameter(name = "instrId", description = "介绍人id", required = false,
                    schema = @Schema(implementation = Long.class, type = "Long", example = "123456")),
            @Parameter(name = "source", description = "来源（gzh:表示公众号来源）", required = false,
                    schema = @Schema(implementation = String.class, type = "String", example = "web"))
    })
    @ApiResponse(responseCode = "200", description = "Success",
            content = @Content(schema = @Schema(implementation = ResultUserVo.class)))
    @GetMapping(value = "/loginPhone")
    @Tag(name = "登录", description = "登录相关接口")
    public Result<UserVo> loginPhone(HttpServletRequest request,
                                     @RequestParam("phone") String phone,
                                     @RequestParam("code") String code,
                                     @RequestParam(value = "wechatCode", required = false) String wechatCode,
                                     @RequestParam(value = "instrId", required = false) Long instrId,
                                     @RequestParam(value = "source", required = false) String source) throws Exception {
        return Result.success(loginService.loginPhone(request, phone, code, false, wechatCode, instrId, source));
    }

    @ApiOperationSupport(order = 12, author = "alex")
    @Operation(summary = "微信登录（小程序）", description = "微信登录（小程序）")
    @LogRestRequest(apiName = "微信登录（小程序）")
    @Parameters({
            @Parameter(name = "code", description = "编码", required = true,
                    schema = @Schema(implementation = String.class, type = "String", example = "dsfsda")),
            @Parameter(name = "phone", description = "手机号", required = false,
                    schema = @Schema(implementation = String.class, type = "String", example = "18288888888")),
            @Parameter(name = "instrId", description = "介绍人id", required = false,
                    schema = @Schema(implementation = Long.class, type = "Long", example = "dsfsda")),
            @Parameter(name = "source", description = "来源（gzh:表示公众号来源）", required = false,
                    schema = @Schema(implementation = String.class, type = "String", example = "web"))
    })
    @ApiResponse(responseCode = "200", description = "Success",
            content = @Content(schema = @Schema(implementation = ResultUserVo.class)))
    @Tag(name = "登录", description = "登录相关接口")
    @GetMapping("/loginWechat")
    public Result<UserVo> weChatCallback(HttpServletRequest request,
                                         @RequestParam(value = "code") String code,
                                         @RequestParam(value = "phone", required = false) String phone,
                                         @RequestParam(value = "instrId", required = false) Long instrId,
                                         @RequestParam(value = "source", required = false) String source) throws Exception {
        // 返回用户信息
        return Result.success(loginService.loginWechat(request, code, false, phone, instrId, source));
    }

    @ApiOperationSupport(order = 20, author = "alex")
    @Operation(summary = "登出", description = "登出")
    @LogRestRequest(apiName = "登出")
    @ApiResponse(responseCode = "200", description = "Success",
            content = @Content(schema = @Schema(implementation = ResultBoolean.class)))
    @GetMapping(value = "/logoutSystem")
    @Tag(name = "登录", description = "登录相关接口")
    public Result<Boolean> logout(HttpServletRequest request) throws Exception {
        return loginService.logout(request);
    }
}
